package com.netdisk.controller.interceptor;
/**
 * 上下文拦截器, 拦截用户的请求, 然后把项目的
 * 上下文路径保持在session里面
 * 保存用户的
 * @author chenlong
 */

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@Component("contextPathInterceptor")
public class ContextPathInterceptor extends AbstractInterceptor{
	
	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		HttpServletRequest request = (HttpServletRequest) ai.getInvocationContext()
				.get(ServletActionContext.HTTP_REQUEST);
		
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName();
		if(request.getServerPort() != 80){
			basePath += (":" + request.getServerPort()
				+ path);
		}
		basePath += (path + "/");
		String imagePath = basePath + "img";
		HttpSession session = request.getSession();
		session.setAttribute("path", path);
		session.setAttribute("basePath", basePath);
		session.setAttribute("imagePath", imagePath);
		return ai.invoke();
	}
	
}
